﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.Membership.BLL;
using BF.Model;
using BF.Membership.MembershipEF;
using WebDataControls;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;

namespace BF.Web.pages.BaseSystem.RolePermissionManagement.RolesManagement
{
    public partial class RolesManagerEdit : BasePage
    {
        #region 实例
        Isys_RoleInfoBLL isys_RoleInfoBLL = IOCFactory.GetIOCResolve<Isys_RoleInfoBLL>();
        AspnetRolesBLL rolesBLL = new AspnetRolesBLL();

        #endregion
        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RoleId.Value = Request.
                    QueryString["skey"] == null ? ""
                    : Request.QueryString["skey"].ToString();
                SetModel();
            }
        }
        /// <summary>
        /// 页面保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            aspnet_Roles roles = GetModel(new aspnet_Roles());

            //角色ID为空时添加用户
            if (String.IsNullOrEmpty(RoleId.Value))
            {
                if (System.Web.Security.Roles.RoleExists(roles.RoleName))
                {
                    Messages("角色名称已占用，请修改!(角色为系统全局配置)", this.Updatepanel1);
                    return;
                }

                if (isys_RoleInfoBLL.AddRole(new RoleView()
                {
                    RoleName = roles.RoleName,
                    Description = roles.Description,
                    gCreateUserId = GetSessionCurrentUserInfo.gUserID,
                }
                    , GetSessionCurrentUserInfo))//AddModel(roles))
                    Messages("新增成功!",
                        "function(e){winer.RefurbishParent();}",
                        this.Updatepanel1);
                else
                    Messages("新增失败!", this.Updatepanel1);
            }
            else
            {
                if (UpdateModel(roles))
                    Messages("更新成功!",
                        "function(e){winer.RefurbishParent();}",
                        this.Updatepanel1);
                else Messages("更新失败!", this.Updatepanel1);
            }
        }
        #endregion

        #region 自定义方法

        #region 设置页面实体
        private void SetModel()
        {
            if (!String.IsNullOrEmpty(RoleId.Value))
            {
                aspnet_Roles roles = new aspnet_Roles();
                roles = rolesBLL.GetModel(new Guid(RoleId.Value));
                RoleName.Enabled = false;
                WebUIHelper.SetControlsValue(roles, "aspnet_Roles", this.Form);
            }
        }
        #endregion


        #region 获取页面实体

        private aspnet_Roles GetModel(aspnet_Roles roles)
        {
            roles = (aspnet_Roles)WebUIHelper.GetControlsModel(roles, "aspnet_Roles", this.Form);
            return roles ?? new aspnet_Roles();
        }
        #endregion

        #region 添加实体
        private bool AddModel(aspnet_Roles roles)
        {
            return rolesBLL.CreateRoles(roles, User.Identity.Name);
        }
        #endregion

        #region 更新实体
        private bool UpdateModel(aspnet_Roles roles)
        {
            roles.RoleId = new Guid(RoleId.Value);
            return rolesBLL.Update(roles);
        }
        #endregion

        #endregion
    }
}